package yunwo.cn.yzstation.view.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.base.BasicAdapter;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.bean.ItemlistBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.utils.StringUtil;

/**
 * Created by lao on 2017/10/11.
 */
public class LoadedActivity extends MyBaseActivity implements View.OnClickListener{

    private List<ItemlistBean.DataBean> mDataBeanList;
    private ListView mListview;
    private LinearLayout mAllchoose;
    private ImageView mSelectimg;
    private TextView mRemove;
    private myadapter mMyadapter;
    private boolean selectAll = true;
    private TextView mSelectTv;
    private List<ItemlistBean.DataBean> mSelectedCollectedCars;
    private ImageView mBack;
    private String mDepartId;
    private Dialog mMyDialog;
    boolean isDelete = false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDataBeanList = (List<ItemlistBean.DataBean>) getIntent().getSerializableExtra("data");
        mDepartId = getIntent().getStringExtra("departId");
        initView();
    }

    private void initView() {
        mListview = (ListView) findViewById(R.id.listview);
        mAllchoose = (LinearLayout) findViewById(R.id.allchoose);
        mSelectimg = (ImageView) findViewById(R.id.selectimg);
        mRemove = (TextView) findViewById(R.id.remove);
        mSelectTv = (TextView) findViewById(R.id.selectTv);
        mBack = (ImageView) findViewById(R.id.back);
        mSelectedCollectedCars = new ArrayList<>();
        mMyadapter = new myadapter(LoadedActivity.this);
        mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
        mListview.setAdapter(mMyadapter);
        mBack.setOnClickListener(this);
        mRemove.setOnClickListener(this);
        mAllchoose.setOnClickListener(this);
    }

    @Override
    protected String settiltile() {
        return "已配载";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_yipeizai;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:

                if (isDelete) {
                    Intent intent = new Intent(LoadedActivity.this, ShoudongActivity.class);
                    intent.putExtra("data", (Serializable) mSelectedCollectedCars);
                    setResult(MyConstants.Resultrxuanding,intent);
                }

                finish();

                break;
            case R.id.remove:


                Map<String, Object> map = new HashMap<>();
                map.put("departDetailList", mSelectedCollectedCars);
                map.put("Id", mDepartId);
                HttpManager.hpptPostString3(MyConstants.stowageremove,map).execute(new GLCallBack() {
                    @Override
                    public void onResponseT(String result, int code, String msg, int id) {
                        if (code == 200) {
                            isDelete = true;
                            Toast.makeText(LoadedActivity.this, "移除成功", Toast.LENGTH_SHORT).show();
                            for (int i = 0; i < mSelectedCollectedCars.size(); i++) {
                                if (mDataBeanList.contains(mSelectedCollectedCars.get(i))) {
                                    mDataBeanList.remove(mSelectedCollectedCars.get(i));
                                }
                            }
                            if (mDataBeanList.size() == 0) {
                                mListview.setVisibility(View.GONE);
                            }
                            mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);

                            Intent intent1 = new Intent(LoadedActivity.this, ShoudongActivity.class);
                            intent1.putExtra("data", (Serializable) mSelectedCollectedCars);
                            setResult(MyConstants.Resultrxuanding,intent1);
                            finish();


                        }else {
                            Toast.makeText(LoadedActivity.this, msg, Toast.LENGTH_SHORT).show();
                            return;
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(LoadedActivity.this, "移除失败，服务器异常", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    @Override
                    public void onAfter(int id) {
                        if (mMyDialog != null && mMyDialog.isShowing()) {
                            mMyDialog.dismiss();
                        }
                    }

                    @Override
                    public void onBefore(Request request, int id) {
                        if (mMyDialog == null) {
                            mMyDialog = ProgressDialog.show(LoadedActivity.this, null, "请稍等...");
                            mMyDialog.setCancelable(false);
                            mMyDialog.show();
                        }
                    }
                });





             /*   for (int i = 0; i < mSelectedCollectedCars.size(); i++) {
                    if (mDataBeanList.contains(mSelectedCollectedCars.get(i))) {
                        mDataBeanList.remove(mSelectedCollectedCars.get(i));
                    }
                }
                if (mDataBeanList.size() == 0) {
                    mListview.setVisibility(View.GONE);
                }
                mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);*/

               /* Intent intent1 = new Intent(LoadedActivity.this, ShoudongActivity.class);
                intent1.putExtra("data", (Serializable) mSelectedCollectedCars);
                setResult(MyConstants.Resultrxuanding,intent1);
                finish();*/
                break;
            case R.id.allchoose:
                if (mDataBeanList.size() > 0) {
                    if (selectAll) {
                        mSelectTv.setText("取消全选");
                        mSelectimg.setImageResource(R.drawable.btn_gou);
                        mSelectedCollectedCars.addAll(mDataBeanList);
                        selectAll = false;
                    }else {
                        mSelectTv.setText("全选");
                        mSelectimg.setImageResource(R.drawable.btn_quanxuan);
                        mSelectedCollectedCars.clear();
                        selectAll = true;
                    }
                }
                mMyadapter.refreshDataAndNotifyDataSetChanged(mDataBeanList);
                break;
        }
    }

    class myadapter extends BasicAdapter<ItemlistBean.DataBean> {

        public myadapter(Context mctx) {
            super(mctx);
        }

        @Override
        protected int setItemLayout() {
            return R.layout.item_yizhuangzai;
        }

        @Override
        protected void bindData(View convertView, ItemlistBean.DataBean data, final int position) {

            TextView waybill = get(convertView, R.id.waybill);
            TextView consignee = get(convertView, R.id.consignee);
            TextView destination = get(convertView, R.id.destination);
            final ImageView state = get(convertView, R.id.state);
            RelativeLayout autoRL = get(convertView, R.id.autoRL);

            waybill.setText(data.getSubTrackingNumber());
            consignee.setText(data.getConsigneeName());
            destination.setText(data.getEndOrganName());

            if (mDataBeanList.size() >0) {

                if (mSelectedCollectedCars.contains(mDataBeanList.get(position))) {// 点击全部
                    state.setImageResource(R.drawable.btn_gou);

                } else {
                    state.setImageResource(R.drawable.btn_quanxuan);
                }
                autoRL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!mSelectedCollectedCars.contains(mDataBeanList.get(position))) {
                            state.setImageResource(R.drawable.btn_gou);
                            mSelectedCollectedCars.add(mDataBeanList.get(position));
                        }else {
                            state.setImageResource(R.drawable.btn_quanxuan);
                            mSelectedCollectedCars.remove(mDataBeanList.get(position));
                        }
                        if (mSelectedCollectedCars.size() == mDataBeanList.size()) {
                            mSelectTv.setText("取消全选");
                            mSelectimg.setImageResource(R.drawable.btn_gou);
                        }else {
                            mSelectTv.setText("全选");
                            mSelectimg.setImageResource(R.drawable.btn_quanxuan);

                        }
                    }
                });
            }
        }
    }
}
